package com.linkhub.oms.admin.modules.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.admin.modules.user.domain.dto.UserDTO;
import com.linkhub.oms.admin.modules.user.domain.param.MerchantEditParam;
import com.linkhub.oms.admin.modules.user.domain.vo.UserInfoVO;
import com.linkhub.oms.admin.modules.user.domain.vo.UserInfoWrapper;
import com.linkhub.oms.admin.modules.user.service.UserBizService;
import com.linkhub.oms.admin.modules.whitelist.service.WhiteListApprovalBizService;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.util.QtBeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description: UserController
 * @Author: echo
 * @Date: 2024/3/6 16:03
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"用户模块接口"})
@RequestMapping("/user")
@SaCheckLogin
public class UserController {

    @Resource
    private UserBizService userBizService;
    @Resource
    private WhiteListApprovalBizService whiteListApprovalBizService;

    @GetMapping("/info")
    @ApiOperation(value = "查询用户信息接口")
    public DataResponse<UserInfoWrapper> getUserInfo() {

        return DataResponse.success(userBizService.assembleUserInfoInSaSession());
    }

    @PostMapping("/vip")
    @ApiOperation(value = "立即申请接口")
    @SaCheckRole(value = RoleConstant.CUSTOMER)
    public DataResponse<Void> applyVipCustomer() {
        whiteListApprovalBizService.applyVipCustomer(LhSessionContext.getUserEmail());
        return DataResponse.success();
    }

    @GetMapping("/merchant/list")
    @ApiOperation(value = "查询所有商家接口")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE, RoleConstant.FINANCE, RoleConstant.STORE_STAFF}, mode = SaMode.OR)
    public DataResponse<List<UserInfoVO>> listMerchants() {
        List<UserDTO> users = userBizService.listMerchants();
        return DataResponse.success(QtBeanUtil.copyList(users, UserInfoVO::new));
    }

    @PutMapping("/info")
    @ApiOperation(value = "更新商家信息")
    @SaCheckRole(value = RoleConstant.CUSTOMER)
    public DataResponse<Void> updateUserInfo(@Valid @RequestBody MerchantEditParam editParam) {
        userBizService.updateUserInfo(editParam);
        return DataResponse.success();
    }





}
